<template>
  <div class="rightbox" v-cloak>
    <h1>级别修改</h1>
    <div class="add-box" style="width:650px;">
      <el-form ref="form" :model="form" label-width="110px">
        <el-form-item
          label="名称"
          prop="name"
          :rules="[
            { required: true, message: '不能为空', trigger: 'blur' }
          ]"
        >
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item
          label="价格"
          prop="price"
          :rules="[
            { required: true, message: '不能为空', trigger: 'blur' }
          ]"
        >
          <el-input style="width:60px" v-model="form.price"></el-input>
        </el-form-item>
        <el-form-item
          label="状态"
          prop="state"
          :rules="[
            { required: true, message: '不能为空', trigger: 'blur' }
          ]"
        >
          <el-radio v-model="form.state" label="1">启用</el-radio>
          <el-radio v-model="form.state" label="-1">禁用</el-radio>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit('form')">提交</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
import qs from 'qs'
export default {
  data () {
    return {
      form: {
        price: '',
        name: '',
        state: ''
      },
      numberindex: ''
    }
  },
  methods: {
    getParams () {
      var that = this
      that.numberindex = that.$route.params.numberindex
      that.$pub.open()
      if (that.numberindex) {
        axios.post('/gp/Number/Eidt', qs.stringify({
          numberindex: that.numberindex
        })
        )
          .then((res) => {
            that.$pub.close()
            if (res.data.code === 0) {
              that.form.price = res.data.data.price
              that.form.name = res.data.data.name
              that.form.state = res.data.data.state + ''
            } else {
              that.$pub.failcode(that, res.data.code, res.data.data)
            }
          })
          .catch(function () {
            that.$pub.catchbox(that)
          })
      } else {
        that.$router.push({
          name: 'numberList'
        })
      }
    },
    submit: function (form) {
      var that = this
      that.$pub.open()
      that.$refs[form].validate((valid) => {
        if (valid) {
          axios.post('/gp/Number/EidtPost', qs.stringify({
            numberindex: that.numberindex,
            price: that.form.price,
            name: that.form.name,
            state: that.form.state
          })
          )
            .then((res) => {
              that.$pub.close()
              if (res.data.code === 0) {
                that.$alert('修改成功', '提示', {
                  confirmButtonText: '确定',
                  callback: action => {
                    that.$router.push({
                      name: 'numberList'
                    })
                  }
                })
              } else {
                that.$pub.failcode(that, res.data.code, res.data.data)
              }
            })
            .catch(function () {
              that.$pub.catchbox(that)
            })
        }
      })
    },
    resetForm: function () {
      var that = this
      that.form.price = ''
      that.form.name = ''
    }
  },
  mounted () {
    this.getParams()
  }
}
</script>
